package org.shunya.duplicatefileperquisitor.gui;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.fxml.JavaFXBuilderFactory;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Modality;
import javafx.stage.Stage;
import org.shunya.duplicatefileperquisitor.model.FileTypeExtensions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;

import static java.nio.file.Files.newInputStream;

public class DuplicateFilePerquisitorMain extends Application {

    private Stage primaryStage;
    private Stage fileTypesStage;
    private Stage fileMaskStage;
    private final double MINIMUM_WINDOW_WIDTH = 1213.0;
    private final double MINIMUM_WINDOW_HEIGHT = 877.0;


    @Override
    public void start(Stage primaryStage) throws Exception {
        try {
            this.primaryStage = primaryStage;
            this.primaryStage.setTitle("Duplicate File Perquisitor");
            this.primaryStage.setMinWidth(MINIMUM_WINDOW_WIDTH);
            this.primaryStage.setMinHeight(MINIMUM_WINDOW_HEIGHT);
            this.primaryStage.setResizable(false);
            initApp();
            primaryStage.show();
        } catch (Exception ex) {
            Logger.getLogger(DuplicateFilePerquisitorMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void initApp() {
        try {
            gotoFileScanner();
        } catch (Exception ex) {
            Logger.getLogger(DuplicateFilePerquisitorMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void gotoFileScanner() throws Exception {
        DuplicateFilePerquisitorController perquisitorController = (DuplicateFilePerquisitorController) replaceSceneContent(primaryStage, "./src/main/java/org/shunya/duplicatefileperquisitor/gui/DuplicateFilePerquisitor.fxml", 1213, 877);
        perquisitorController.setApplication(this);
    }

    void gotoFileTypes(TextField fileTypesTxt) throws Exception {
        fileTypesStage = new Stage();
        fileTypesStage.setTitle("Duplicate File Perquisitor");
        fileTypesStage.setResizable(false);
        FileTypesDialogController fileTypesDialogController = (FileTypesDialogController) replaceSceneContent(fileTypesStage, "./src/main/java/org/shunya/duplicatefileperquisitor/gui/FileTypesDialog.fxml", 680, 435);
        fileTypesDialogController.setApplication(this);
        fileTypesDialogController.setFileTypesTxt(fileTypesTxt);
        fileTypesDialogController.setStage(fileTypesStage);
        fileTypesStage.initModality(Modality.WINDOW_MODAL);
        fileTypesStage.initOwner(this.primaryStage);
        fileTypesStage.showAndWait();
    }

    void gotoFileMaskAndGetFileTypeExtensions(FileTypeExtensions fileTypeExtensions) throws Exception {
        fileMaskStage = new Stage();
        fileMaskStage.setTitle("Duplicate File Perquisitor");
        fileMaskStage.setResizable(false);
        FileMaskDialogController fileMaskDialogController = (FileMaskDialogController) replaceSceneContent(fileTypesStage, "./src/main/java/org/shunya/duplicatefileperquisitor/gui/FileMaskDialog.fxml", 407, 118);
        fileMaskDialogController.setFileTypesTxt(fileTypeExtensions);
        fileMaskDialogController.setStage(fileMaskStage);
        fileMaskStage.initModality(Modality.WINDOW_MODAL);
        fileMaskStage.initOwner(this.primaryStage);
        //fileTypesStage.toBack();
        fileMaskStage.show();
    }

    private Initializable replaceSceneContent(Stage stage, String fxml, int width, int height) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        InputStream in = newInputStream(FileSystems.getDefault().getPath(fxml));
        loader.setBuilderFactory(new JavaFXBuilderFactory());
        loader.setLocation(DuplicateFilePerquisitorMain.class.getResource(fxml));
        AnchorPane page;
        try {
            page = (AnchorPane) loader.load(in);
        } finally {
            in.close();
        }
        Scene scene = new Scene(page, width, height);
        scene.getStylesheets().add(DuplicateFilePerquisitorMain.class.getResource("./../../../../css/DuplicateFilePerquisitor.css").toExternalForm());
        //scene.getStylesheets().addAll(DuplicateFilePerquisitorMain.class.getResource("./DuplicateFilePerquisitor.css").toExternalForm());
        stage.setScene(scene);
        stage.sizeToScene();
        return loader.getController();
    }

    public static void main(String[] args) {
        Application.launch(DuplicateFilePerquisitorMain.class, (java.lang.String[])null);
    }

}
